<template>
    <div class="signeList">
        <Row>
            <Col :xs="24">
                <div class="margin-up-down">
                    <Button type="info" @click="add" class="btn">
                        <i class="iconfont icon-add" style="font-size: 14px; margin-right: 5px" ></i>
                    </Button>
                </div>
                <div class="margin-up-down">
                    <Select v-model="info.hospitalId" clearable style="width:155px" placeholder="请选择迁入机构" @on-change='changeHospital'>
                        <Option v-for="item in hospitalList" :value="item.id" :key="item.id" style='text-align:center'>{{ item.orgName }}</Option>
                    </Select> 
                </div>
                <div class="margin-up-down">
                    <Select v-model="info.teamId" clearable style="width:155px" placeholder="请选择迁入团队" >
                        <Option v-for="item in doctorGroupList" :value="item.id" :key="item.id" style='text-align:center'>{{ item.name }}</Option>
                    </Select> 
                </div>
                <div class="margin-up-down">
                    <Select class="w-select" clearable v-model="info.fdsStatus" placeholder="签约状态">
                        <Option
                            v-for="item in signStatus"
                            :value="item.id"
                            :key="item.id"
                            style='text-align:center'
                        >{{item.name}}</Option>
                    </Select>
                </div>
                <div class="margin-up-down">
                    <DatePicker type="daterange" v-model="time" placement="bottom-end" placeholder="请选择签约时间" separator='至' format="yyyy-MM-dd" style="width: 200px"></DatePicker>
                </div>
                <div class="margin-up-down">
                    <Input class="w-input" v-model="info.name" placeholder="请输入患者姓名"/>                                            
                </div>
                <div class="margin-up-down">
                    <Input class="w-input" v-model="info.idNo" placeholder="请输入身份证号"/>                                            
                </div>
                <div class="margin-up-down">
                    <Button type="primary" @click="loadPage(1)" class="btn">
                        <Icon type="ios-search" size="14"/>
                    </Button>
                </div>
                <div class="margin-up-down">
                    <Button icon="icon-gengxin" @click="reset" class="btn">
                        <i class = "iconfont icon-gengxin" style="font-size:16px;margin-left:-8px;"></i>
                    </Button>
                </div>
            </Col>
        </Row>
        <p class="num">
            <span>已迁入人数：{{moveInNum}} 人</span>
            <span>已签约人数：{{signingNum}} 人</span>
            <span>签约率：{{signingRate}} </span>
        </p>
        <Table class="m-table" stripe :columns="columns" :data="signList" @on-selection-change="mySelectRow"></Table>
        <div class="page">
            <Page :total="count" :current='info.pageNo' :page-size="info.pageSize" show-total show-elevator @on-change="loadPage"/>
        </div>
        <Modal
            v-model="ModalStatus"
            title = '电子健康档案迁出-提示'
            width="600"
            :mask-closable="false"
            footer-hide
        >
            <div class = 'signe'>
                <p class="number">您已选择 <span class="tishi">（{{patient}}）</span>电子健康档案，是否要迁出<span class="tishi">（{{teamName}}）</span>家庭医生团队？</p>
            </div>
            <div style='margin-top:10px;width:200px;margin:10px auto;'>
                <Button type="primary" @click="emigration" :disabled='disabledSave' class="btn">
                    <Icon type="ios-checkmark" style="margin-right: 5px" />确定
                </Button>
                <Button style="marginLeft: 20px" @click="back" class="btn">
                    <i class = "iconfont icon-butongyi" style="font-size:14px;margin-right:5px"></i>取消
                </Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import { DatePicker } from "view-design";
import api from "@/api/commonApi";
import Utils from "@/utils";
import {encrypt,decrypt} from "@/plugins/qwAes.js";
export default { 
    data() {
        return {
            hospitalList: [],
            doctorGroupList: [],
            info: {
                hospitalId: null,
                teamId: null,
                fdsStatus: '',
                contractStartTime: '',
                contractEndTime: '',
                name: '',
                idNo: '',
                moveInStatus: 1,           //1:已迁入数据 2:未迁入数据
                pageNo: 1,          
                pageSize: 10,          
            },
            time: [],
            searchKey: "",
            signStatus: [
                {id: -2, name:'待签约'},
                {id: -1, name:'待付款'},
                {id: 0, name:'签约审核中'},
                {id: 1, name:'生效中'},
                {id: 2, name:'签约拒绝'},
                {id: 3, name:'已过期'},
                {id: 4, name:'已解约'},
                {id: 5, name:'系统自动拒绝'},
                {id: 6, name:'取消'},
                {id: 7, name:'退款中'},
                {id: 8, name:'待生效'},
                {id: 9, name:'签约中'},
                {id: 10, name:'解约中'},
            ],
            columns: [
                {
                    title: "排序",
                    key: "sum",
                    width: 80,
                    align: "center",
                    render: (h, params) => {
                        let sum = (this.info.pageNo - 1) * this.info.pageSize + params.index + 1;
                        return h("span", sum);
                    },
                },
                {
                    title: "姓名",
                    key: "name",
                    align: "center",
                },
                {
                    title: "身份证号",
                    key: "idNo",
                    align: "center",
                    render: (h, params) => {
                        if (params.row.idNo) {
                            // let idcard = String(params.row.idcard).replace(/(\d{6})\d{8}(\d{4})/g,'$1********$2');
                            // return idcard
                            var str1 = params.row.idNo.slice(0, 6);
                            var str2 = "";
                            var str3 = params.row.idNo.slice(14);
                            if (params.row.idNo.length == 15) {
                                str2 = "*****";
                            } else {
                                str2 = "********";
                            }
                            let idNo = str1 + str2 + str3;
                            return idNo;
                        }
                    },
                },
                { title: "迁入团队", key: "teamName", align: "center", width: 200, },
                { title: "签约机构", key: "fdsOrgName", align: "center", 
                    render: (h, params) => {
                        let contractStartTime = params.row.contractStartTime;
                        if(contractStartTime) {
                            return h("span", {}, params.row.fdsOrgName);
                        } else {
                            return h("span", {}, '');
                        }                      
                    }
                },
                {
                    title: "签约状态",
                    key: "statusText",
                    align: "center",
                    render: (h, params) => {
                        let fdsStatus = params.row.fdsStatus;
                        let statusText;
                        this.signStatus.forEach(i =>{
                            if(i.id==fdsStatus) {
                                statusText = i.name
                            }
                        })
                        return h("span", {}, statusText);
                    }
                },
                {
                    title: "建档机构",
                    key: "crOrgName",
                    align: "center",
                },
                {
                    title: "建档时间",
                    key: "crDate",
                    align: "center",
                    render: (h, params) => {
                        let crDate = params.row.crDate.substring(0,10);
                        if(crDate) {
                            return h("span", {}, crDate);
                        } else {
                            return h("span", {}, '');
                        }                      
                    }
                },
                {
                    title: "签约时间",
                    key: "contractStartTime",
                    align: "center",
                    render: (h, params) => {
                        let contractStartTime = params.row.contractStartTime.substring(0,10);
                        if(contractStartTime) {
                            return h("span", {}, contractStartTime);
                        } else {
                            return h("span", {}, '');
                        }                      
                    }
                },
                {
                    title: "设置",
                    key: "iOperate",
                    width: 200,
                    align: "center",
                    render: (h, params) => {
                        let row = params.row;
                        // let tkey = encrypt("idNo=" + row.idNo);
                        return h("div", [
                            row.fdsStatus == 3 || row.fdsStatus == 4 || row.fdsStatus == -2
                                ? h(
                                      "span",
                                      {
                                          props: {
                                              type: "primary",
                                              size: "small",
                                          },
                                          style: {
                                              color: "#21c780",
                                              margin: "0px 6px",
                                          },
                                          on: {
                                              click: () => {
                                                  this.move(row);
                                              },
                                          },
                                      },
                                      "迁出"
                                  )
                                : null,
                            row.fdsStatus == 3 || row.fdsStatus == 4 || row.fdsStatus == -2 ? "|" : null,
                            h(
                                "span",
                                {
                                    props: {
                                        size: "small",
                                    },
                                    style: {
                                        color: "#5362e1",
                                        margin: "0px 6px",
                                    },
                                    on: {
                                        click: () => {
                                            window.open('http://3sy2830370.imdo.co/qw/healthRecord.html?Authorization=' + encrypt("idNo=340323201311050102") +'&invoker=1#/health');
                                        },
                                    },
                                },
                                "EHR"
                            ),
                            "|",
                            h(
                                "span",
                                {
                                    props: {
                                        type: "error",
                                        size: "small",
                                    },
                                    style: {
                                        color: "#fc6c49",
                                        marginLeft: "6px",
                                    },
                                    on: {
                                        click: () => {
                                            window.open('http://3sy2830370.imdo.co/qw/browser.html?Authorization=' + encrypt("idNo=130432198901210127") +'&invoker=1#/overview');
                                        },
                                    },
                                },
                                "EMR"
                            )
                        ]);
                    },
                },
            ],
            signList: [],
            count: 0,
            pageSize: 10,
            pageNo: 1,
            disabledSave: false,
            ModalStatus: false,
            moveInNum: '',          //迁入数
            signingNum: '',       //签约人数
            signingRate: '',        //签约率
            teamName: '',
            patient: '',
            baseId: '',
        };
    },
    components: {
        DatePicker,
    },
    created() {
        this.hospital = this.$route.query.hospital
            ? this.$route.query.hospital
            : null;
        let breadList = [
            { path: "/index", title: "首页" },
            {
                path: "/index/operation/ordersmanagement/index",
                title: "订单管理"
            },
            {
                path: "/index/comprehensive/registration/list",
                title: "预约挂号订单"
            }
        ];
        this.$emit("changeBreadList", breadList);
    },
    mounted() {
        let pageNo = this.$route.query.pageNo;
        pageNo = pageNo ? pageNo : 1;
        //上来就加载第一页数据
        this.loadPage(1);
        this.getHospital()
        this.loadDoctorGroup()
        this.getsignNum()
    },
    methods: {
        //获取医疗机构
        getHospital() {
            let body = {
                pageNo: 1,
                pageSize: 1000,
                hospitalType: 1,
                haveFdsTeam: 1,
            };
            this.$axios.post(api.mechanismregList, body).then(res => {
                if (res.data.code) {
                    let ret = res.data.object;
                    if(ret.records){
                        this.hospitalList = ret.records;
                    } else {
                        this.hospitalList = []
                    }
                } 
            }).catch(() => {

            });
        },
        //医生小组管理-分页列表
        loadDoctorGroup() {
            var params = {};
            params.hospitalId= this.info.hospitalId ? this.info.hospitalId : null;
            params.pageNo = 1;
            params.pageSize = 1000;  
            params.type = 1;  
            this.$axios.post(api.findTeamDtoList,params).then(res => {
                if(res.data.success) {
                    let ret = res.data.object
                    if(ret.records) {
                        this.doctorGroupList = ret.records
                    } else {
                        this.doctorGroupList = []
                    }                  
                }   
            })
        },
        changeHospital() {
            this.info.teamId = ''
            this.loadDoctorGroup()
        },
        add() {
            this.functionJS.queryNavgationTo(
                this,
                '/index/publicOperation/healthDocument/unsigneList',
            );
        },
        //加载列表数据
        loadPage(pageNo) {
            this.info.pageNo = pageNo;
            // let startDate = this.time ? new Date(this.time[0]) : '';
            // let endDate = this.time ? new Date(this.time[1]) : '';
            this.info.contractStartTime = Utils.getNowDateBySpecialFormat(this.time[0]);
            this.info.contractEndTime = Utils.getNowDateBySpecialFormat(this.time[1]);
            // params.searchKey = this.searchKey.trim() ? this.searchKey.trim() : null;
            this.$axios.post(api.pbasePage, this.info).then(resp => {
                    if (resp.data.success) {
                        let ret = resp.data.object;
                        this.count = ret.total
                        if(ret.records){
                            this.signList = ret.records;
                        } else {
                            this.signList = [];
                        }                       
                    } 
                })
                .catch(err => {
                    console.log(err);
                });
                this.getsignNum()
        },
        //签约情况统计
        getsignNum() {
            let params = {
                hospitalId: this.info.hospitalId ? this.info.hospitalId : '',
                teamId: this.info.teamId ? this.info.teamId : '',
            }
            this.$axios.post(api.findSigningRate, params).then(resp => {
                if (resp.data.success) {
                    let ret = resp.data.object;
                    this.moveInNum = ret.moveInNum             
                    this.signingNum = ret.signingNum             
                    this.signingRate = ret.signingRate             
                }
            })
            .catch(err => {
                console.log(err);
            });
        },
        //迁出
        move(row) {
            this.ModalStatus = true
            this.teamName = row.teamName
            this.patient = row.name
            this.baseId = row.id
        },
        //确定迁出档案
        emigration() {
            this.disabledSave = true
            this.$axios.post(api.pbaseMoveOut, {baseId: this.baseId }).then(resp => {
                if (resp.data.success) {
                    this.$Message.info(resp.data.message);
                    this.disabledSave = false
                    this.ModalStatus = false  
                    this.loadPage(1);           
                } else {
                    this.disabledSave = false
                }
            })
            .catch(err => {
                this.disabledSave = false
                this.$Message.error(resp.data.object.message);
            });
        },
        back() {
            this.ModalStatus = false
        },
        //清除
        reset() {
           this.info = {}
           this.time = []
           this.info.moveInStatus = 1
           this.info.pageSize = 10
           this.loadPage(1);
        },
    }
};
</script>
<style lang="less" scoped>
.signeList {
    padding: 10px;
    width: 100%;
    background: #ffffff;
    box-sizing: border-box;
    min-height: 600px;
    .w-select {
        width: 100px;
    }
    .w-input {
        width: 220px;
    }
    .m-table {
        margin: 10px 0;
    }
    .margin-up-down {
        display: inline-block;
        margin: 10px 0;
    }
    .btn {
        width: 50px;
        height: 32px;
        line-height: 18px;
    }
    .page {
        width: 100%;
        margin: 10px auto;
        text-align: center;
    }
    .num {
        padding: 10px 0;
        span {
            margin-right: 80px;
        }
    }
}
.signe {
    width:90%;
    height:auto;
    display:flex;
    margin:0 auto;
    flex-direction:column;
    .number {
        font-size: 14px;
        font-weight: 550;
        text-align: center;
        margin-bottom: 20px;
    }
    .tishi {
        color: #5362e1;
    }
}
</style>